﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Config.Console
{
    public class CommandLineOptions
    {
        private string _infile;
        private string _outfile;
        private string _basedir;
        private IDictionary<string, string> _props = new Dictionary<string, string>();

        #region Properties
        /// <summary>
        /// Gets the input file.
        /// </summary>
        /// <value>The input file.</value>
        public string InFile 
        { 
            get { return _infile;}
        }

        /// <summary>
        /// Gets the output file.
        /// </summary>
        /// <value>The output file.</value>
        public string OutFile 
        {
            get { return _outfile;}
        }

        /// <summary>
        /// Gets the base directory.
        /// </summary>
        /// <value>The base directory.</value>
        public string BaseDirectory 
        {
            get { return _basedir;  }

        }

        /// <summary>
        /// Gets the comamnd line properties.
        /// </summary>
        /// <value>The comamnd line  properties.</value>
        public IDictionary<string, string> Properties 
        { 
            get
            {
                return _props;
            }
        }
        #endregion

        /// <summary>
        /// Parses the command line arguments.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        /// <returns>The parsed command line options.</returns>
        public static CommandLineOptions Parse(string[] args)
        {
            var options = new CommandLineOptions();
            for (int i = 0; i < args.Length; i++)
            {
                var index = args[i].IndexOf(":");
                if (index == -1)
                    throw new Exception(String.Format("Badly formed command line option :", args[i]));

                var optionKey = args[i].Substring(0, index).ToLower();
                index++;
                var optionVal = args[i].Substring(index, args[i].Length - index);

                switch (optionKey)
                {
                    case "/infile":
                        options._infile = new FileInfo(optionVal).FullName;
                        break;

                    case "/outfile":
                        options._outfile = new FileInfo(optionVal).FullName;
                        break;

                    case "/basedir":
                        // If the dir path ends with a \ the comma is passed in as well.
                        if (optionVal.EndsWith("\""))
                            optionVal = optionVal.Substring(0, optionVal.Length - 1);

                        options._basedir = new DirectoryInfo(optionVal).FullName;
                        break;

                    case "/prop":
                        i++;
                        if (args.Length <= i)
                            throw new Exception("Invalid command line properties format.");

                        options.Properties.Add(optionKey, args[i]);
                        break;

                    default:
                       throw new Exception(String.Format("Unrecognised command line option: {0}", optionKey));
                }

            }

            return options;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineOptions"/> class.
        /// </summary>
        private CommandLineOptions()
        {
        }

    }
}
